****************************************************************************************************************
**																											  **
** This do file is part of the replication material for the following article: 								  **
**  "Is There an Ideological Asymmetry in the Incumbency Effect? Evidence from U.S. Congressional Elections." **
** 		Authors: Morisi, Davide, Jost, John, Panagopoulos, Costas, and Jussi Valtonen		    			  **
** 		Journal: Social Psychological and Personality Science																		  **
**																											  **
** This file replicates the following step of the analysis: 												  **
**																											  **
**		RECODING CCES 2016 ORIGINAL DATASET			   													  	  **
**																											  **
****************************************************************************************************************

*Set working directory
cd "C:\Users\Davide Morisi\Dropbox\datasets\CCES"

*Open original dataset
*Ansolabehere, Stephen; Schaffner, Brian F., 2017, "CCES Common Content, 2016", https://doi.org/10.7910/DVN/GDF6Z0, Harvard Dataverse, V4, UNF:6:WhtR8dNtMzReHC295hA4cg== [fileUNF] 
*Downloaded on January 16, 2020 from https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi%3A10.7910/DVN/GDF6Z0
use "2016\CCES16_Common_OUTPUT_Feb2018_VV.dta", clear

***
*covariates

*gender
fre gender // 2=female
*age
sum birthyr
*education
fre educ 
*race
fre race
recode race 4/8=99, gen(racer) // 99=other
*religion
fre religpew
recode religpew 3/8=3 12=3 9=4 10=5 11=6, gen(religpewr)
fre religpewr
*economy
fre CC16_302
rename CC16_302 economy
*state
fre inputstate
rename inputstate state
clonevar state16 = state
*weight
sum commonweight_vv commonweight_vv_post commonweight_vv_lgbt commonweight commonweight_post
rename commonweight_vv_post weight1


*********************
*partisanship / ideology

*party of respondent - profile
*3 categories
*1 democrat, 2 republican, 3 independent, 4 other, 5 dk
fre pid3
*7 categories
*1 strong dem, 2 not strong dem, 3 lean dem, 4 indep, 5 lean rep, 6 not strong rep, 7 strong rep, 8 dk 
fre pid7
ta pid7 pid3 // leaning partisans coded as independent/other/dk

*party id post
*3 categories (note: cat 5 dk missing)
fre CC16_421a // pid3
rename CC16_421a pid3post
*5 categories
fre CC16_421b // leaners
rename CC16_421b leaners_prof_16
ta pid3post if leaners_prof_16!=. // asked only for independents/others + 1 republican
fre pid3post leaners_prof_16
gen pid5post = 1 if pid3post==1
replace pid5post = 2 if pid3post>2 & leaners_prof_16==1
replace pid5post = 3 if pid3post==3 & leaners_prof_16==3
replace pid5post = 4 if pid3post>2 & leaners_prof_16==2
replace pid5post = 5 if pid3post==2
*1 dem, 2 lean dem, 3 indep, 4 lean rep, 5 rep
fre pid5post

*party registration pre-election
fre CC16_360

*ideology profile
fre ideo5

*ideology from PRE-ELECTION SURVEY
*1 very lib, 2 lib, 3 somewhat lib, 4 mod, 5 somewhat con, 6 con, 7 very con, 8dk
fre CC16_340a
rename CC16_340a ideo7


****
*vote post
*turnout
fre CC16_401
rename CC16_401 turnout16
clonevar turnout = turnout16
*senate
fre CC16_410b
rename CC16_410b votesen16
clonevar vote_sen = votesen16
*house
fre CC16_412
rename CC16_412 votehou16
clonevar vote_hou = votehou16
*president vote post
fre CC16_410a


******
*contextual var SENATE

*incumbent candidates
des SenCand1Incumbent_post SenCand2Incumbent_post
encode(SenCand1Incumbent_post), gen(SenCand1Incumbent_postr)
encode(SenCand2Incumbent_post), gen(SenCand2Incumbent_postr)
fre SenCand1Incumbent_postr SenCand2Incumbent_postr
recode SenCand1Incumbent_postr 1=0 2=1
recode SenCand2Incumbent_postr 1=0 2=1
label de incumb 0"not incumbent" 1"incumbent", modify
label values SenCand1Incumbent_postr incumb
label values SenCand2Incumbent_postr incumb
fre SenCand1Incumbent_postr SenCand2Incumbent_postr
ta SenCand1Incumbent_postr SenCand2Incumbent_postr

*party of candidate
des SenCand1Party_post SenCand2Party_post
fre SenCand1Party_post SenCand2Party_post
encode SenCand1Party_post, gen(SenCand1Party_postr)
encode SenCand2Party_post, gen(SenCand2Party_postr)
fre SenCand1Party_postr SenCand2Party_postr
label de partycand 1"dem. candidate" 2"rep. candidate" 3"other candidate", modify
label val SenCand1Party_postr partycand
label val SenCand2Party_postr partycand

*republican vs dem incumbent
fre SenCand1Party_postr SenCand1Incumbent_postr
fre SenCand2Party_postr SenCand2Incumbent_postr
gen inc_rep_sen = 0 if (SenCand1Party_postr==1 & SenCand2Party_postr==2) & (SenCand1Incumbent_postr==1 & SenCand2Incumbent_postr==0)
replace inc_rep_sen = 1 if (SenCand1Party_postr==1 & SenCand2Party_postr==2) & (SenCand1Incumbent_postr==0 & SenCand2Incumbent_postr==1)
fre inc_rep_sen
*including districts with no dem/rep incumbents
ta SenCand1Incumbent_postr SenCand2Incumbent_postr if (SenCand1Party_postr==1 & SenCand2Party_postr==2)
gen inc_all_sen = inc_rep_sen
recode inc_all_sen 0=-1
replace inc_all_sen = 0 if (SenCand1Party_postr==1 & SenCand2Party_postr==2) & (SenCand1Incumbent_postr==0 & SenCand2Incumbent_postr==0)
fre inc_all_sen // -1=dem inc, 0=inc not dem nor rep, 1=rep inc

*keep only states with elections, not open elections
ta state inc_rep, nol
*filter
gen count=1 if inc_rep!=.
egen count_sum = sum(count), by(state)
fre state if count_sum<10 & inc_rep!=.
ta state inc_rep if count_sum<=10, nol
ta state inc_rep if count_sum>10, nol
gen filter_sen=1 if inc_rep!=.
replace filter_sen=2 if count_sum<10 & inc_rep!=.
replace filter_sen=3 if (state==1 | state==2 | state==4 | state==6 | state==12 | state==16 | state==17 | state==37 | state==45) & inc_rep==0
replace filter_sen=3 if (state==6 | state==8 | state==36) & inc_rep==1
ta filter_sen
ta state inc_rep if filter_sen==1
clonevar filter_sen16 = filter_sen


***
*contextual var HOUSE

*incumbent candidates
des HouseCand1Incumbent_post HouseCand2Incumbent_post
fre HouseCand1Incumbent_post HouseCand2Incumbent_post
encode(HouseCand1Incumbent_post), gen(HouseCand1Incumbent_postr)
encode(HouseCand2Incumbent_post), gen(HouseCand2Incumbent_postr)
fre HouseCand1Incumbent_postr HouseCand2Incumbent_postr
recode HouseCand1Incumbent_postr 1=0 2=1
recode HouseCand2Incumbent_postr 1=0 2=1
label val HouseCand1Incumbent_postr incumb
label val HouseCand2Incumbent_postr incumb
fre HouseCand1Incumbent_postr HouseCand2Incumbent_postr
ta HouseCand1Incumbent_postr HouseCand2Incumbent_postr

*party of candidate
des HouseCand1Party_post HouseCand2Party_post
fre HouseCand1Party_post HouseCand2Party_post
encode HouseCand1Party_post, gen(HouseCand1Party_postr)
encode HouseCand2Party_post, gen(HouseCand2Party_postr)
fre HouseCand1Party_postr HouseCand2Party_postr
recode HouseCand1Party_postr 1=1 5=2 2/4=3
recode HouseCand2Party_postr 1=3 2=1 3/5=3 6=2
label val HouseCand1Party_postr partycand
label val HouseCand2Party_postr partycand
fre HouseCand1Party_postr HouseCand2Party_postr

*republican incumbent vs. dem incumbent
*note: with this var we restrict analysis to seats with dem vs. rep AND with inc. vs. not inc.
fre HouseCand1Party_postr HouseCand1Incumbent_postr
fre HouseCand2Party_postr HouseCand2Incumbent_postr
ta HouseCand1Party_postr HouseCand2Party_postr // no cases when candidate 1 is republican and candidate 2 is democrat
ta HouseCand1Incumbent_postr HouseCand2Incumbent_postr if (HouseCand1Party_postr==1 & HouseCand2Party_postr==2)
gen inc_rep_hou = 0 if (HouseCand1Party_postr==1 & HouseCand2Party_postr==2) ///
& (HouseCand1Incumbent_postr==1 & HouseCand2Incumbent_postr==0)
replace inc_rep_hou = 1 if (HouseCand1Party_postr==1 & HouseCand2Party_postr==2) ///
& (HouseCand1Incumbent_postr==0 & HouseCand2Incumbent_postr==1)
fre inc_rep_hou
*including districts with no dem/rep incumbents
ta HouseCand1Incumbent_postr HouseCand2Incumbent_postr if (HouseCand1Party_postr==1 & HouseCand2Party_postr==2) 
gen inc_all_hou = inc_rep_hou
recode inc_all_hou 0=-1
replace inc_all_hou = 0 if (HouseCand1Party_postr==1 & HouseCand2Party_postr==2) & (HouseCand1Incumbent_postr==0 & HouseCand2Incumbent_postr==0)
fre inc_all_hou

*keep only states with elections, not open elections
ta state inc_rep_hou
*filter
gen count1=1 if inc_rep_hou!=.
egen count_sum1 = sum(count1), by(state HouseCand1Name_post)
gen filter_hou=1 if inc_rep_hou!=.
replace filter_hou=2 if count_sum1<10 & inc_rep_hou!=.
ta state inc_rep_hou if filter_hou==1
*double check
bys state: ta HouseCand1Name_post if filter_hou==1 // e.g. wyoming missing because no incumbent
ta state if HouseCand1Name_post=="Tom Nelson" // seat without incumbent
ta state if HouseCand1Name_post=="Tom Nelson" & filter_hou==1 // removed when filter applied
*remove wrong coding of Joe DeVivo as democrat, instead of write-in candidate
replace filter_hou=2 if HouseCand1Name_post=="Joe DeVivo"

ta state inc_rep_hou if filter_hou==1
clonevar filter_hou16 = filter_hou



*******************
*combined variables > vote + incumbent status

*vote + incumbent status, all options - SENATE
ta vote_sen inc_all_sen
fre turnout vote_sen
fre inc_all_sen // -1=dem inc, 0=inc not dem nor rep, 1=rep inc
gen votinc_sen_all = 0 if turnout<5 | vote_sen==5 // did not vote
replace votinc_sen_all = 10 if vote_sen==1 & inc_all_sen==-1 & SenCand1Party_postr==1
replace votinc_sen_all = 11 if vote_sen==1 & inc_all_sen==0 & SenCand1Party_postr==1
replace votinc_sen_all = 12 if vote_sen==1 & inc_all_sen==1 & SenCand1Party_postr==1
replace votinc_sen_all = 20 if vote_sen==2 & inc_all_sen==-1 & SenCand2Party_postr==2
replace votinc_sen_all = 21 if vote_sen==2 & inc_all_sen==0 & SenCand2Party_postr==2
replace votinc_sen_all = 22 if vote_sen==2 & inc_all_sen==1 & SenCand2Party_postr==2
*label var votinc_sen_all "Voted for dem/rep by incumbent status - SENATE"
*label de votinc_sen_all 0"did not vote" 10"vote dem, dem inc"  11"vote dem, no inc"  12"vote dem, rep inc" ///
*20"vote rep, dem inc"  21"vote rep, no inc"  22"vote rep, rep inc" 
*label val votinc_sen_all votinc_sen_all
fre votinc_sen_all

*vote + incumbent status, all options - HOUSE
ta vote_hou inc_all_hou
fre vote_hou
fre inc_all_hou // -1=dem inc, 0=inc not dem nor rep, 1=rep inc
gen votinc_hou_all = 0 if turnout<5 | vote_hou==8  // did not vote
replace votinc_hou_all = 10 if vote_hou==1 & inc_all_hou==-1 & HouseCand1Party_postr==1
replace votinc_hou_all = 11 if vote_hou==1 & inc_all_hou==0 & HouseCand1Party_postr==1 
replace votinc_hou_all = 12 if vote_hou==1 & inc_all_hou==1 & HouseCand1Party_postr==1
replace votinc_hou_all = 20 if vote_hou==2 & inc_all_hou==-1 & HouseCand2Party_postr==2
replace votinc_hou_all = 21 if vote_hou==2 & inc_all_hou==0 & HouseCand2Party_postr==2
replace votinc_hou_all = 22 if vote_hou==2 & inc_all_hou==1 & HouseCand2Party_postr==2
*label var votinc_hou_all "Voted for dem/rep by incumbent status - HOUSE"
*label val votinc_hou_all votinc_sen_all
fre votinc_hou_all


***
gen year = 2016

rename V101 caseid16
clonevar caseid = caseid16

ta tookpost


****
*create congressional districts
decode state, gen(stated)
drop if stated=="District of Columbia"
encode stated, gen(staten)
label var staten "States from 1 to 50 in alphabetical order"
fre staten // alphabetical order, from 0 to 50

*combine with district
des cdid115_post
rename cdid115_post cd2016str
encode cd2016str, gen(cd2016)
fre cd2016str // problem value 1 instead of 01, 2 instead of 02, etc.
fre cd2016str if staten==2

*turn 1 into 01, 2 into 02 etc.
gen cd2016_first = (substr(cd2016str,1,1))
gen cd2016_second = (substr(cd2016str,2,1))
encode cd2016_first, gen(cd2016_firsten)
encode cd2016_second, gen(cd2016_seconden)
fre cd2016_first if cd2016_seconden==.
gen value0 = 0
egen cd2016_first_correct=concat(value0 cd2016_first) if cd2016_firsten!=. & cd2016_seconden==.
fre cd2016_first cd2016_first_correct if cd2016_seconden==.

gen cd2016str_correct = cd2016_first_correct if cd2016_seconden==.
replace cd2016str_correct = cd2016str if cd2016_seconden!=.
label var cd2016str_correct "Post  Congressional district 2016 1=01"
fre cd2016str cd2016str_correct if staten==2
fre cd2016str cd2016str_correct if staten==9
fre cd2016str cd2016str_correct if staten==50

egen stcd=concat(staten cd2016str_correct)

*double check > all correct
fre stcd if staten==9 // florida
fre stcd if staten==9 & cd2016!=.
fre stcd if staten==45 // vermont
fre stcd if staten==45 & cd2016!=.

*
rename stcd stcd_string
gen stcd = real(stcd_string) // turn into numeric
label var stcd_string "State (1 to 50 alphabetical) + congr. district (01 to n) STRING"
label var stcd "State (1 to 50 alphabetical) + congr. district (01 to n)"

***
/*merge with contextual var

save "CCES_2016_recoded2.dta", replace
import excel "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Other files\hr4618.xls", sheet("2016") firstrow clear
sum year stcd inc pwin dv dvp fr po1 po2 redist dexp rexp dpres switchb switcha
drop year switchb switcha
save "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Replication do files\Data\house_data_2016.dta", replace
use "CCES_2016_recoded2.dta", clear
*/

*merge info about previous election
merge m:1 stcd using "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Replication do files\Data\house_data_2014.dta" // all matched from using
drop pwin dv dvp fr redist dexp rexp dpres _merge
rename inc inc_previous
rename po1 po1_previous
rename po2 po2_previous

*post election
merge m:1 stcd using "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Replication do files\Data\house_data_2018.dta" // all matched
drop pwin dv dvp fr redist dexp rexp dpres _merge
rename inc inc_post
rename po1 po1_post
rename po2 po2_post

merge m:1 stcd using "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Replication do files\Data\house_data_2016.dta" // all matched from using
drop _merge

***
*voter registration status post
fre votereg_post
recode votereg_post (2 3=0 "No/DK") (1=1 "Yes"), gen(register_post)
label var register_post "Registered to vote? (post)"
fre register_post

***
*job approval
*Please indicate whether you approve or disapprove of the job that each of the following are doing.
* 1 Strongly approve
* 2 somewhat Approve
* 3 somewhat Disapprove
* 4 Strongly disapprove
* 5 Not sure

*House representative
fre CC16_320f 
rename CC16_320f approval_house

*senate
*2 senators for each state. 
fre CC16_320g CC16_320h 
rename CC16_320g approval_sen1
rename CC16_320h approval_sen2

***
*keep only selected variables
keep gender birthyr educ racer religpewr state state16 weight1 economy ///
pid3 pid7 pid3post pid5post leaners_prof_16 ///
ideo5 ideo7 ///
turnout turnout16 vote_sen votesen16 vote_hou votehou16 ///
SenCand1Incumbent_postr SenCand2Incumbent_postr SenCand1Party_postr SenCand2Party_postr ///
inc_rep_sen inc_all_sen filter_sen filter_sen16 ///
HouseCand1Incumbent_postr HouseCand2Incumbent_postr HouseCand1Party_postr HouseCand2Party_postr ///
inc_rep_hou inc_all_hou filter_hou filter_hou16 ///
votinc_sen_all votinc_hou_all ///
year caseid caseid16 tookpost ///
staten ///
cd2016 cd2016str cd2016str_correct stcd_string stcd ///
inc pwin dv dvp fr po1 po2 redist dexp rexp dpres ///
register_post approval_house approval_sen1 approval_sen2 ///
inc_previous po1_previous po2_previous ///
inc_post po1_post po2_post


*save recoded file
save "CCES_2016_recoded2.dta", replace


